// ==UserScript==
// @name         编程猫
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       Wang
// @match        https://festival-task.codemao.cn/h5/zyAdmin/*
// @run-at document-start
// @grant        none
// @grant        unsafeWindow
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==

;(function () {
  document.onkeydown = function (e) {
    console.log('键盘', e)
  }
  // console.log(window.location)
  //获取页面url
  //console.log('aaaaa',window.location.href)
  var FinishList = JSON.parse(localStorage.getItem('FinishList')) || [
    { title: 'aaa', context: 'bbb' }
  ]
  var UnFinishList = JSON.parse(localStorage.getItem('UnFinishList')) || [
    { title: 'aaa', context: 'bbb' }
  ]
  var ChallengeList = JSON.parse(localStorage.getItem('ChallengeList')) || [
    { title: 'aaa', context: 'bbb' }
  ]
  var UnChallengeList = JSON.parse(localStorage.getItem('UnChallengeList')) || [
    { title: 'aaa', context: 'bbb' }
  ]
  // localStorage.setItem('COMMENT',JSON.stringify(list))
  //异步  等待数据请求过后获取页面内容

  //定义定时器
  var intervalId = null

  start()
  //获取姓名
  function getName() {
    return $('#app .app-wrapper .el-col-5')[2].innerText.replace(
      '学生姓名：',
      ''
    )
  }
  //定义添加按钮事件
  function addButton() {
    //普通
    const f = $('.el-tabs--top')[0]
    const f1 = document.createElement('button')
    const f2 = document.createElement('button')
    f1.innerText = '完成'
    f2.innerText = '未完成'
    f1.id = 'f1'
    f2.id = 'f2'
    f.appendChild(f1)
    f.appendChild(f2)
    //跳过
    const father = $('.el-row .el-form .el-form-item')[0]
    const button1 = document.createElement('button')
    const button2 = document.createElement('button')
    const button3 = document.createElement('button')
    const button4 = document.createElement('button')
    button1.innerText = '完成提交'
    button2.innerText = '未完成提交'
    button3.innerText = '添加评语'
    button4.innerText = '添加挑战任务评语'
    button1.id = 'finish'
    button2.id = 'unfinish'
    button3.id = 'addcontext'
    button4.id = 'challenge'
    father.appendChild(button1)
    father.appendChild(button2)
    father.appendChild(button3)
    father.appendChild(button4)
  }
  //添加评语
  function AddContext() {
    const finishAll = $('.el-radio-group .el-radio .el-radio__input')[0]
      .getAttribute('class')
      .includes('is-checked') //勾选的是否是完成
    // $('.el-textarea__inner')[0].value  获取输入框的内容
    const context = $('.el-textarea__inner')[0].value
    const title = GetTitle()
    //获取class名
    if (finishAll) {
      //已经完成的评语
      //判断是否已经有评语了
      const index = FinishList.findIndex((item) => item.title == title)
      console.log('index', index)
      if (index >= 0) {
        alert('覆盖原来已完成的评语')

        FinishList[index].context = context
        localStorage.setItem('FinishList', JSON.stringify(FinishList))
      } else {
        FinishList.push({ title: title, context: context })
        localStorage.setItem('FinishList', JSON.stringify(FinishList))
        alert('已完成评语添加成功')
      }
    } else {
      //未完成的评语
      console.log('添加未完成')
      //判断是否已经有评语了
      const index = UnFinishList.findIndex((item) => item.title == title)
      console.log('index', index)
      if (index >= 0) {
        alert('覆盖原来未完成的评语')
        setInput('.el-textarea__inner', '')
        UnFinishList[index].context = context
        localStorage.setItem('UnFinishList', JSON.stringify(UnFinishList))
      } else {
        UnFinishList.push({ title: title, context: context })
        localStorage.setItem('UnFinishList', JSON.stringify(UnFinishList))
        alert('未完成评语添加成功')
        setInput('.el-textarea__inner', '')
      }
    }
  }
  //普通任务完成
  function ok() {
    //普通任务
    //本课标题
    const title = GetTitle()
    const obj = FinishList.filter((item) => item.title == title)[0]
    console.log('obj', obj)

    const Input = $('.el-textarea__inner')[0] //获取输入框
    if (!obj) {
      alert('没有评语,请先添加评语')
      return
    }
    if (
      obj.context.includes('挑战任务') ||
      obj.context.includes('挑战作品') ||
      obj.context.includes('挑战题')
    ) {
      alert('评语出错了,再添加一下')
      return
    }
    //$('.el-radio-group .el-radio .el-radio__input')[number].click() //将number改成0 是勾选通过  1 是不通过 2 是无推荐评语
    // 控制是否通过
    $('.el-radio-group .el-radio .el-radio__input')[0].click() //勾选通过
    $('.el-radio-group .el-radio .el-radio__input')[2].click() //勾选无

    /*                                      在此处更改选择的标签                        */

    setTimeout(() => {
      $('.el-checkbox-group .el-checkbox .el-checkbox__input')[0].click()
      setTimeout(() => {
        $('.el-checkbox-group .el-checkbox .el-checkbox__input')[1].click()
        setTimeout(() => {
          $('.el-checkbox-group .el-checkbox .el-checkbox__input')[2].click()
          setTimeout(() => {
            $('.el-checkbox-group .el-checkbox .el-checkbox__input')[3].click()
            setTimeout(() => {
              $(
                '.el-checkbox-group .el-checkbox .el-checkbox__input'
              )[4].click()
              setTimeout(() => {
                $(
                  '.el-checkbox-group .el-checkbox .el-checkbox__input'
                )[5].click()
              })
            })
          })
        })
      })
    })

    try {
      setInput('.el-textarea__inner', obj.context)
    } catch (err) {}
  }
  //挑战任务完成
  function chok() {
    const title = GetTitle()
    const obj = ChallengeList.filter((item) => item.title == title)[0]
    const Input = $('.el-textarea__inner')[0] //获取输入框
    if (!obj) {
      return alert('没有挑战任务评语,请先添加评语')
    }
    //$('.el-radio-group .el-radio .el-radio__input')[number].click() //将number改成0 是勾选通过  1 是不通过 2 是无推荐评语
    // 控制是否通过
    $('.el-radio-group .el-radio .el-radio__input')[0].click() //勾选通过
    $('.el-radio-group .el-radio .el-radio__input')[2].click() //勾选无

    /*                                               在此处更改选择的标签                        */

    setTimeout(() => {
      $('.el-checkbox-group .el-checkbox .el-checkbox__input')[0].click()
      setTimeout(() => {
        $('.el-checkbox-group .el-checkbox .el-checkbox__input')[1].click()
        setTimeout(() => {
          $('.el-checkbox-group .el-checkbox .el-checkbox__input')[2].click()
          setTimeout(() => {
            $('.el-checkbox-group .el-checkbox .el-checkbox__input')[3].click()
            setTimeout(() => {
              $(
                '.el-checkbox-group .el-checkbox .el-checkbox__input'
              )[4].click()
              setTimeout(() => {
                $(
                  '.el-checkbox-group .el-checkbox .el-checkbox__input'
                )[5].click()
              })
            })
          })
        })
      })
    })

    try {
      setInput('.el-textarea__inner', obj.context)
    } catch (err) {}
  }
  //普通任务未完成
  function del() {
    const title = GetTitle()
    const obj = UnFinishList.filter((item) => item.title == title)[0]

    if (!obj) {
      return alert('没有评语,请先添加评语')
    }

    $('.el-radio-group .el-radio .el-radio__input')[1].click() //勾选通过
    $('.el-radio-group .el-radio .el-radio__input')[2].click() //勾选无

    /*                                                            在此处更改选择的标签                        */
    setTimeout(() => {
      $('.el-checkbox-group .el-checkbox .el-checkbox__input')[0].click()
      setTimeout(() => {
        $('.el-checkbox-group .el-checkbox .el-checkbox__input')[1].click()
        setTimeout(() => {
          $('.el-checkbox-group .el-checkbox .el-checkbox__input')[2].click()
          setTimeout(() => {
            $('.el-checkbox-group .el-checkbox .el-checkbox__input')[3].click()
            setTimeout(() => {
              $(
                '.el-checkbox-group .el-checkbox .el-checkbox__input'
              )[4].click()
              setTimeout(() => {
                $(
                  '.el-checkbox-group .el-checkbox .el-checkbox__input'
                )[5].click()
              })
            })
          })
        })
      })
    })

    try {
      setInput('.el-textarea__inner', obj.context)
    } catch (err) {}
  }
  //挑战任务未完成
  function chdel() {
    const title = GetTitle()
    const obj = UnChallengeList.filter((item) => item.title == title)[0]
    const Input = $('.el-textarea__inner')[0] //获取输入框
    if (!obj) {
      return alert('没有挑战任务评语,请先添加评语')
    }
    // 控制是否通过
    $('.el-radio-group .el-radio .el-radio__input')[1].click() //勾选通过
    $('.el-radio-group .el-radio .el-radio__input')[2].click() //勾选无

    /*                                           在此处更改选择的标签                        */
    setTimeout(() => {
      $('.el-checkbox-group .el-checkbox .el-checkbox__input')[0].click()
      setTimeout(() => {
        $('.el-checkbox-group .el-checkbox .el-checkbox__input')[1].click()
        setTimeout(() => {
          $('.el-checkbox-group .el-checkbox .el-checkbox__input')[2].click()
          setTimeout(() => {
            $('.el-checkbox-group .el-checkbox .el-checkbox__input')[3].click()
            setTimeout(() => {
              $(
                '.el-checkbox-group .el-checkbox .el-checkbox__input'
              )[4].click()
              setTimeout(() => {
                $(
                  '.el-checkbox-group .el-checkbox .el-checkbox__input'
                )[5].click()
              })
            })
          })
        })
      })
    })

    try {
      setInput('.el-textarea__inner', obj.context)
    } catch (err) {}
  }
  //完成不提交
  function f1() {
    if ($('.el-col-10 span')[0].innerText.includes('挑战任务')) {
      //挑战任务
      chok() //挑战任务完成
    } else {
      ok() //普通完成
    }
  }
  //未完成不提交
  function f2() {
    if ($('.el-col-10 span')[0].innerText.includes('挑战任务')) {
      chdel() //挑战任务未完成
    } else {
      del() //未完成
    }
  }
  //全部完成提交
  function Finished() {
    f1()
    setTimeout(() => {
      $('.el-button--success')[1].click()
    })
  }
  //未完成并且提交
  function UnFinish() {
    f2()
    setTimeout(() => {
      $('.el-button--success')[1].click()
    })
  }

  //获取本课题目
  function GetTitle() {
    console.log('获取题目')
    return $('.el-breadcrumb__inner')[5].innerText
  }
  //获取评语
  function GetList() {
    return JSON.parse(localStorage.getItem('FinishList'))
  }
  //开启定时器
  function start() {
    console.log('开启定时器')
    if (intervalId == null) {
      intervalId = setInterval(() => {
        if (
          window.location.href ===
            'https://festival-task.codemao.cn/h5/zyAdmin/?#/teacher/work' ||
          window.location.href ===
            'https://festival-task.codemao.cn/h5/zyAdmin/#/teacher/work'
        ) {
          addAll()
          stop()
        }
      }, 1000)
    }
  }
  //关闭定时器
  function stop() {
    console.log('关闭定时器')
    clearInterval(intervalId)
    intervalId = null
  }
  //添加所有
  function addAll() {
    const timer = setInterval(() => {
      console.log('addAll')
      if ($('#app .app-wrapper .el-col-5')[2]) {
        clearInterval(timer)
        //clearInterval(timer)
        //添加点击按钮
        Open()
        addButton()
        //给完成按钮添加点击事件  触发全部完成函数
        $('#finish')[0].addEventListener('click', Finished)
        $('#unfinish')[0].addEventListener('click', UnFinish)
        $('#addcontext')[0].addEventListener('click', AddContext)
        $('#challenge')[0].addEventListener('click', Addchallenge)
        $('#f1')[0].addEventListener('click', f1)
        $('#f2')[0].addEventListener('click', f2)
        $('.el-button--success')[1].addEventListener('click', () => {
          setTimeout(() => {
            if ($('.el-textarea__inner').val() <= 50) {
              return
            }
          })
        })

        $('.el-button--warning')[1].addEventListener('click', () => {
          //确认退出按钮
          setTimeout(() => {
            start()
          })
        })
        $('.el-button--warning')[0].addEventListener('click', () => {
          //跳过
          setTimeout(() => {
            $('.el-button--success')[2].addEventListener('click', () => {
              setTimeout(() => {
                start()
              })
            })
          })
        })
        console.log('22期')
        //  if($('.el-breadcrumb__inner')[4].innerText.includes('第22期')){
        //     alert('云鹏老师')
        //  }
        console.log(
          'aaaa',
          $('.el-col-10 span')[0].innerText.includes('挑战任务')
        )
        $('.el-button--success')[1].addEventListener('click', () => {
          setTimeout(() => {
            start()
          })
        })
        document.onkeydown = function (e) {
          console.log('键盘', e)
          if (e.keyCode == 81) {
            //q 完成提交
            Finished()
          } else if (e.keyCode == 69) {
            //e 未完成 提交
            UnFinish()
          } else if (e.keyCode == 32) {
            //空格  跳过
            $('.el-button--warning')[0].click()
            setTimeout(() => {
              $('.el-input__inner')[1].focus()
              document.execCommand('insertText', false, 1)
              setTimeout(() => {
                $('.el-button--success')[2].click()
              })
            })
          } else if (e.keyCode == 75) {
            //k  提交
            console.log($('.el-button--success')[1].click())
          } else if (e.keyCode == 74) {
            //j 完成 不提交
            $('#f1')[0].click()
          } else if (e.keyCode == 76) {
            //l 未完成 不提交
            $('#f2')[0].click()
          }
        }
      }
    }, 1000)
  }
  //打开作业
  function Open() {
    console.log('open', $('.el-col-10 .el-button--text')[0])
    if ($('.el-col-10 .el-button--text')[0]) {
      $('.el-col-10 .el-button--text')[0].click()
      try {
        console.log('aaa')
      } catch (err) {
        console.dir(err)
      }
    } else if ($('.el-col-10 a span')[0]) {
      $('.el-col-10 a span')[0].click()
      Run()
    }
    //$('.el-col-10 .el-button--text')[0].click()
    // $('.el-col-10 a span')[0].click()
  }
  //向富文本添加内容
  function setInput(selector, text) {
    setTimeout(() => {
      $(selector)[0].focus()
      document.execCommand('insertText', false, text)
    })
  }

  function Run() {}
  //添加挑战任务评语
  function Addchallenge() {
    const finishAll = $('.el-radio-group .el-radio .el-radio__input')[0]
      .getAttribute('class')
      .includes('is-checked') //勾选的是否是完成
    // $('.el-textarea__inner')[0].value  获取输入框的内容
    const context = $('.el-textarea__inner')[0].value
    const title = GetTitle()
    //获取class名
    if (finishAll) {
      //已经完成的评语
      //判断是否已经有评语了
      const index = ChallengeList.findIndex((item) => item.title == title)
      console.log('index', index)
      if (index >= 0) {
        alert('覆盖原来已完成的评语')
        setInput('.el-textarea__inner', '')
        ChallengeList[index].context = context
        localStorage.setItem('ChallengeList', JSON.stringify(ChallengeList))
      } else {
        ChallengeList.push({ title: title, context: context })
        localStorage.setItem('ChallengeList', JSON.stringify(ChallengeList))
        alert('已完成评语添加成功')
      }
    } else {
      //未完成的评语
      console.log('添加未完成')
      //判断是否已经有评语了
      const index = UnChallengeList.findIndex((item) => item.title == title)
      console.log('index', index)
      if (index >= 0) {
        alert('覆盖原来未完成的评语')
        setInput('.el-textarea__inner', '')
        UnChallengeList[index].context = context
        localStorage.setItem('UnChallengeList', JSON.stringify(UnChallengeList))
      } else {
        UnChallengeList.push({ title: title, context: context })
        localStorage.setItem('UnChallengeList', JSON.stringify(UnChallengeList))
        alert('未完成评语添加成功')
      }
    }
  }
})()
